<template>
	<view class="con">
		<u--textarea v-model="value" placeholder="请输入链接,无需转换自动识别,支持200多平台去水印,解析失败请复制网址部分."
			border="bottom"></u--textarea>
		<u-button color="linear-gradient(to right, rgb(66, 83, 216), rgb(213, 51, 186))" type="success" text="解析" @click="submit"></u-button>
		<ad class="bottomAd" unit-id="adunit-c7aa122b438bd36f"></ad>
		<view class="subsection">
			<u-subsection :list="list" :current="current" @change="subChage()"></u-subsection>
		</view>
		<view class="video" v-if="current == 0">
			<video :src="video"></video>
			<view class="btnFlex">
				<view class="btnLeft">
					<u-button color="linear-gradient(to right, rgb(66, 83, 216), rgb(213, 51, 186))" type="success" text="下载" @click="downloadVideo"></u-button>
				</view>
				<view class="btnRight">
					<u-button color="linear-gradient(to right, rgb(213, 51, 186), rgb(66, 83, 216))" type="success" text="看个视频支持一下作者" @click="showAd"></u-button>
				</view>
			</view>
		</view>
		<!-- <view class="img" v-if="current == 1">
			<view class="imgCon">
				<u--image :showLoading="true" :src="src"></u--image>
			</view>
			<u-button type="success" text="下载"></u-button>
		</view> -->
		<view class="text" v-if="current == 2">
			<u--textarea v-model="text"></u--textarea>
		</view>
		<u-popup mode="center" :round="10" :closeOnClickOverlay="false" :show="loading">
			<view class="popupCon">
				<u-loading-icon :text="popText"></u-loading-icon>
			</view>
		</u-popup>
		<!-- <u-loading-page :loading="loading" loading-text="正在解析中,请稍后"></u-loading-page>
		<u-loading-page :loading="loadingDow" loading-text="正在下载中,请稍后"></u-loading-page> -->
		<u-toast ref="uToast"></u-toast>
	</view>
</template>

<script>
	let videoAd = null
	let interstitialAd = null
	export default {
		data() {
			return {
				popText:'解析中，请稍候。。。',
				showNotify: false,
				loadingDow: false,
				loading: false,
				text: '',
				value: '',
				list: ['视频', '文案'],
				current: 0,
				src: 'https://cdn.uviewui.com/uview/album/1.jpg',
				video: '',
			}
		},
		onLoad() {
			uni.authorize({
				scope: 'scope.writePhotosAlbum',
				success() {
					console.log(interstitialAd)
					if (uni.createInterstitialAd) {
					  interstitialAd = uni.createInterstitialAd({
					    adUnitId: 'adunit-a41c4c732bf0bd9f'
					  })
					  interstitialAd.onLoad(() => {
					  })
					  interstitialAd.onError((err) => {
						  console.log(err)
					  })
					  interstitialAd.onClose(() => {})
					}
					
					// 在适合的场景显示插屏广告
					if (interstitialAd) {
					  interstitialAd.show().catch((err) => {
					    console.error(err)
					  })
					}
				}
			})
		},
		methods: {
			subChage(i) {
				this.current = i
			},
			submit() {
				this.loading = true
				// uni.$u.throttle(this.showAd(), 2000)
				uni.$u.throttle(this.aa(), 2000)

			},
			aa() {
				this.popText = '转换地址中，请稍候'
				let url = this.getStrUrl(this.value)
				uni.$u.http.get(
					`https://eeapi.cn/api/video/32FFA2EFB8281A0A5CC938E792AAE5C05209DFBEE5071BE98D/3333/?url=${url}`
				).then(res => {
					this.loading = false
					console.log(res.data)
					if(res.data.code == 202){
						this.$refs.uToast.show({
							type: 'error',
							icon: false,
							title: res.data.msg,
							message: res.data.msg,
							duration: 2000
						})
					}
					this.video = res.data.data.down
					this.src = res.data.data.download_image
					this.text = res.data.data.title
				}).catch(err => {

				})
			},
			showAd() {
				if (wx.createRewardedVideoAd) {
					videoAd = wx.createRewardedVideoAd({
						adUnitId: 'adunit-57d32c569b2cfd03'
					})
					videoAd.onLoad(() => {})
					videoAd.onError((err) => {
						console.log(err)
					})
					videoAd.onClose((res) => {
						
					})
				}
				if (videoAd) {
					videoAd.show().catch(() => {
						// 失败重试
						videoAd.load()
							.then(() => videoAd.show())
							.catch(err => {
								console.log('激励视频 广告显示失败')
							})
					})
				}
			},
			getStrUrl(s) {
				var reg = /(http:\/\/|https:\/\/)((\w|=|\?|\.|\/|&|-)+)/g;
				var reg = /(https?|http|ftp|file):\/\/[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|]/g;
				s = s.match(reg);
				return (s && s.length ? s[0] : null);
			},
			downloadVideo() {
				let _this = this
				this.loading = true
				this.popText = '下载中，请稍候。。。'
				uni.getSetting({
					success(success) {
						console.log(success)
						if (success.authSetting['scope.writePhotosAlbum']) {
							console.log(_this.video)
							uni.downloadFile({
								url: _this.video, //仅为示例，并非真实的资源
								success: (res) => {
									console.log(res)
									_this.loading = false
									uni.saveVideoToPhotosAlbum({
										filePath: res.tempFilePath,
										success: function() {
											_this.$refs.uToast.show({
												type: 'success',
												icon: false,
												title: '保存成功',
												message: "保存成功",
												duration: 2000
											})
										},
										fail: function(e) {
											console.log(`fail---${e}`, e)
										}
									});
								}
							});
						}
					}
				})
			}
		}
	}
</script>

<style>
	.con {
		margin: 0;
		padding: 0
	}

	.subsection {
		margin: 10px 0 0 0
	}

	.video video {
		width: 100%
	}

	.img {
		margin: auto;
	}

	.bottomAd {
		/* position: fixed;
		bottom: 0;
		left: 0 */
		margin:5px 0 0 0;
	}
	
	.popupCon{
		padding: 25px;
	}
	.btnFlex{
		padding:0 10px
	}
	.btnFlex .btnLeft,.btnRight{
		float:left
	}
	.btnFlex .btnLeft{
		width:48%;
		margin: 0 4% 0 0;
	}
	.btnFlex .btnRight{
		width:48%;
	}
</style>